package com.ml.doctor.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.ml.doctor.R;
import com.ml.doctor.fragment.MonitorFragment;
import com.ml.doctor.fragment.MyFragment;
import com.ml.doctor.fragment.PatientFragment;
import com.ml.doctor.fragment.SignFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    Fragment currentFragment;
    int currentIndex = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        hideLeftView();
        setTopTitle("家庭医疗管家");
        radioGroup.setOnCheckedChangeListener(this);

        if (currentIndex == -1) {
            showHideFragemnt(MonitorFragment.class.getName(), null);
            currentIndex = 1;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currentIndex", currentIndex);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        List<String> list = new ArrayList<>();
        list.add(MonitorFragment.class.getName());
        list.add(PatientFragment.class.getName());
        list.add(SignFragment.class.getName());
        list.add(MyFragment.class.getName());
        currentIndex = savedInstanceState.getInt("currentIndex");
        currentFragment = fm.findFragmentByTag(list.get(currentIndex));
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.radio_btn1:
                currentIndex = 0;
                showHideFragemnt(MonitorFragment.class.getName(), null);
                showTopTitle();
                break;
            case R.id.radio_btn2:
                currentIndex = 1;
                showHideFragemnt(PatientFragment.class.getName(), null);
                showTopTitle();
                break;
            case R.id.radio_btn3:
                currentIndex = 2;
                showHideFragemnt(SignFragment.class.getName(), null);
                showTopTitle();
                break;
            case R.id.radio_btn4:
                currentIndex = 3;
                showHideFragemnt(MyFragment.class.getName(), null);
                hideTopTitle();
                break;
        }
    }

    private void showHideFragemnt(String tag, Bundle bundle) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (currentFragment != null)
            fragmentTransaction.hide(currentFragment);
        Fragment fragment = fragmentManager.findFragmentByTag(tag);
        if (fragment == null) {
            fragment = Fragment.instantiate(this, tag, bundle);
            fragmentTransaction.add(R.id.container, fragment, tag);
        } else {
            fragmentTransaction.show(fragment);
        }
        currentFragment = fragment;
        fragmentTransaction.commit();
    }


}
